TeamMod = {}
TeamMod.Teams = {}
team.SetUp( 1 , "Neutral" , Color( 255 , 255 , 255 , 255 ) , true )

TeamMod.Teams[ 1 ] = {
	number = 1,
	Creator = Entity(0),
	CreatorName = "World",
	Password = "",
	name = "Neutral",
	color = Color( 255 , 255 , 255 , 255),
	players = {}
}



if SERVER then 
	AddCSLuaFile( "client/derma.lua" )
	AddCSLuaFile( "teammod.lua" )

	local function CreateAndJoin( ply , command , args )
		if !args[1] || !args[2] || !args[3] || !args[4] then return end
		if #args[1] > 30 then return end
		local iTeam = #TeamMod.Teams + 1

		for i = 2 , #TeamMod.Teams do
			local Creator = TeamMod.Teams[ i ].Creator

			if ply == Creator then
				iTeam = i
			end
		end

		local crc
		local name = args[1]
		local color = Color( args[2] , args[3] , args[4] , 255)
		local Password = args[5]
		if Password then 
			crc = util.CRC(Password)
		else
			crc = ""
		end

		if iTeam == 1001 then //crappy hack to keep from overwriting the joining teams
			for i = 1 , 2 do
				TeamMod.Teams[ 1000 + i ] = {}
			local iTeam = iTeam + 1
			end
		end
		
		team.SetUp( iTeam , name , color , true )
		TeamMod.Teams[ iTeam ] = { 
			number = iTeam,
			name = name,
			Creator = ply,
			CreatorName = ply:Nick(),
			color = color,
			Password = crc,
			players = { }
		}

		umsg.Start( "teammod_addteam" )
			umsg.Short( iTeam )
			umsg.String( name )
			umsg.String( crc )
			umsg.Short( args[2] )
			umsg.Short( args[3] )
			umsg.Short( args[4] )
			umsg.Entity( ply )
		umsg.End()

		if ply.CurTeam != iTeam then
			TeamMod.Teams[ ply.CurTeam ].players[ ply ] = nil 
		end

		ply.CurTeam = iTeam
		TeamMod.Teams[ iTeam ].players[ ply ] = ply
		
		ply:SetTeam( iTeam )
	
		umsg.Start( "teammod_plyjoined" )
			umsg.Short( iTeam )
			umsg.Entity( ply )
		umsg.End()
	end
	concommand.Add( "teammod_create_and_join" , CreateAndJoin )

	local function JoinTeam( ply , command , args )
		if !ply then return end
		if tonumber( args[1] ) > #TeamMod.Teams then return end
		local iTeam = tonumber( args[1] )
		local password = args[2] or ""

		if iTeam then
			if ply.CurTeam != iTeam then
				TeamMod.Teams[ ply.CurTeam ].players[ ply ] = nil 
			end
			if TeamMod.Teams[ iTeam ].Password !="" then
				if TeamMod.Teams[ iTeam ].Password != util.CRC( password ) then return end
			end

			ply.CurTeam = iTeam
			TeamMod.Teams[ iTeam ].players[ ply ] = ply
			
			ply:SetTeam( iTeam )
		
			umsg.Start( "teammod_plyjoined" )
				umsg.Short( iTeam )
				umsg.Entity( ply )
			umsg.End()
		end

	end
	concommand.Add("teammod_join" , JoinTeam)

	local function RefreshTeamMod( ply , cmd , args )
		if !ply:IsUserGroup("owner") || !ply:IsUserGroup("superadmin") || !ply:IsUserGroup("admin") then return end
		TeamMod.Teams = {}
		umsg.Start("teammod_refresh")
		umsg.End()
	end
	concommand.Add("teammod_refresh" , RefreshTeamMod )
	


	local function InitSpawn( ply )
		if !ply.CurTeam then	
			ply:SetTeam( 1 )
			timer.Simple( 1 , function() umsg.Start( "teammod_plyjoined" )
				umsg.Short( 1 )
				umsg.Entity( ply )
			umsg.End()
			end)
			ply.CurTeam = 1
		end
	end
	hook.Add( "PlayerSpawn" , "teammod_MakeNeutral" , InitSpawn )

	local function playershouldtakedamage(victim, attacker)
		if !victim:IsPlayer() || !attacker:IsPlayer() then return end 
		if victim:Team() == attacker:Team() then
			return false
		end
	end
	hook.Add( "PlayerShouldTakeDamage", "playershouldtakedamage", playershouldtakedamage)

	local function OpenMenu( ply )
		umsg.Start("teammod_openmenu" , ply )
		umsg.End()
	end
	hook.Add("ShowSpare1" , "Teammod_openmenu" , OpenMenu)

else

	local function RefreshTeamMod( um )
		TeamMod.Teams = {}
	end
	usermessage.Hook("teammod_refresh" , RefreshTeamMod )

	local function TeamAdd( um )
		local iTeam = um:ReadShort()
		local name = um:ReadString()
		local password = um:ReadString()
		local r , g , b = um:ReadShort() , um:ReadShort(), um:ReadShort()
		local creator = um:ReadEntity()
		
		team.SetUp( iTeam , name , Color( r , g , b , 255) , true )
		TeamMod.Teams[ iTeam ] = { 
			number = iTeam,
			name = name,
			Creator = creator,
			CreatorName = creator:Nick(),
			color = Color( r , g , b , 255 ),
			players = {},
			Password = password,
		}
	end
	usermessage.Hook("teammod_addteam" , TeamAdd)

	local function TeamJoined( um )
		local iTeam = um:ReadShort()
		local ply = um:ReadEntity()
		if ply.CurTeam and ply.CurTeam != iTeam then
			TeamMod.Teams[ ply.CurTeam ].players[ ply ] = nil 
		end
		ply.CurTeam = iTeam
		TeamMod.Teams[ iTeam ].players[ ply ] = ply
	end
	usermessage.Hook( "teammod_plyjoined" , TeamJoined)

end

